using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class EnemySpawn : MonoBehaviour
{
    public float m_fReadyTime = 0.0f;

    public float m_fSpawnTerm = 0.0f;
    public float m_fCurrentSpawnTime = 0.0f;

    public float m_fCurrentTime = 0.0f;
    public int m_iEnemySpawnCount = 0;

    public int m_iWave = 0;
    public int m_iLevel = 0;

    private int m_iCountHead = 0;
    private int m_iCurrentHead = 0;
    private float m_fWaveSpawnTime = 0.0f;
    private float m_fWaveSpawnTerm = 0.0f;


    //Enemy GameObj

    List<GameObject>[] m_EnemyList = new List<GameObject>[2];

    // Level 0 Enemy

    public GameObject m_Lv0Enemy_0;
    public GameObject m_Lv0Enemy_1;
    public GameObject m_Lv0Enemy_2;

    // Level 1 Enemy
    public GameObject m_Lv1Enemy_0;
    public GameObject m_Lv1Enemy_1;
    public GameObject m_Lv1Enemy_2;

    // Use this for initialization
    void Start()
    {
        m_fCurrentSpawnTime = m_fReadyTime;

        m_EnemyList[0] = new List<GameObject>();
        m_EnemyList[1] = new List<GameObject>();

        m_EnemyList[0].Add(m_Lv0Enemy_0);
        //      m_EnemyList[0].Add(m_Lv0Enemy_1);
        //		m_EnemyList [0].Add (m_Lv0Enemy_2);

        m_EnemyList[1].Add (m_Lv1Enemy_0);
        //		m_EnemyList [1].Add (m_Lv1Enemy_1);
        //		m_EnemyList [1].Add (m_Lv1Enemy_2);


    }

    void Update()
    {
        EnemyLevelUp();

        m_fCurrentTime += Time.deltaTime;

        if (m_fCurrentTime >= m_fCurrentSpawnTime)
        {
            if (m_iCountHead == 0)
                m_iCountHead = (int)(Random.Range(m_iEnemySpawnCount - 2, m_iEnemySpawnCount + 2));

            if (m_fWaveSpawnTerm == 0.0f)
                m_fWaveSpawnTerm = Random.Range(0.5f, 2.0f);

            m_fWaveSpawnTime += Time.deltaTime;

            if (m_fWaveSpawnTime >= m_fWaveSpawnTerm)
            {
                int iIndex = Random.Range(0, m_EnemyList[m_iLevel].Count);

                (Instantiate(((m_EnemyList[m_iLevel])[iIndex]),
                            new Vector3(Random.Range(-2f, 2f) + gameObject.transform.position.x,
                        gameObject.transform.position.y, 0.0f), Quaternion.identity) as GameObject).transform.parent = GameObject.Find("Enemies").transform;

                m_fWaveSpawnTime = 0.0f;

                if (m_iCurrentHead >= m_iCountHead)
                {
                    m_fCurrentTime = 0.0f;
                    m_fWaveSpawnTime = 0.0f;
                    m_fWaveSpawnTerm = 0.0f;
                    m_iCountHead = 0;
                    m_iCurrentHead = 0;

                    m_fCurrentTime = 0.0f;
                    m_fCurrentSpawnTime = Random.Range(0, m_fSpawnTerm);

                    m_iEnemySpawnCount += 2;

                    ++m_iWave;
                }

                ++m_iCurrentHead;


            }
        }
    }

    void EnemyLevelUp()
    {
        if(GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().m_fChaos > 10)
            m_iLevel = 1;
    }
}

